﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Billing.WindowsBase;
using System.Xml;
using Infragistics.Win.UltraWinExplorerBar;
using System.Collections;
using System.Reflection;
using System.IO;

namespace Billing.Windows.Shell
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        //System.Collections.Hashtable ht = new System.Collections.Hashtable();

        #region Menu
        public void CreateMenu(string menu)
        {
            XmlDocument docMenu = new XmlDocument();
            docMenu.Load(new StringReader(menu));

            UltraExplorerBarItem item;
            string GroupKey = string.Empty;
            if (docMenu != null)
            {
                Hashtable dllHash = new Hashtable();

                IEnumerator en = docMenu.GetEnumerator();
                Bitmap defaultbitmap = this.Icon.ToBitmap();

                while (en.MoveNext())
                {

                    IEnumerator enGr = ((XmlElement)en.Current).GetEnumerator();

                    while (enGr.MoveNext()) //Groups
                    {
                        XmlElement elGr = (XmlElement)enGr.Current;
                        GroupKey = elGr.GetAttribute("grKEY");
                        if (this.ueb.Groups.Exists(GroupKey) == false)
                        {
                            string ImageName = elGr.GetAttribute("grIcon");

                            //global::StandardBank.Properties.Resources.cancel_32
                            Image img = null;
                            if (ImageName != String.Empty)
                            {
                                img = (Image)global::Billing.Windows.Shell.Properties.Resources.ResourceManager.GetObject(ImageName);
                            }
                            this.ueb.Groups.Add(GroupKey, elGr.GetAttribute("grName")).Settings.AppearancesSmall.HeaderAppearance.Image = img;
                        }

                        IEnumerator enEl = ((XmlElement)enGr.Current).GetEnumerator();

                        while (enEl.MoveNext()) //Elements
                        {
                            XmlElement elEl = (XmlElement)enEl.Current;

                            item = new UltraExplorerBarItem();

                            //'=========================================

                            string dllName = elEl.GetAttribute("dllName");
                            string attributeKey = elEl.GetAttribute("elKEY");
                            string attributeName = elEl.GetAttribute("elName");

                            string dd = Application.StartupPath;

                            Assembly asmb;
                            if (dllHash.Contains(dllName))
                            {
                                asmb = (Assembly)(dllHash[dllName]);
                            }
                            else
                            {
                                if (!File.Exists(Application.StartupPath + "//" + dllName))
                                {
                                    continue;
                                }

                                asmb = Assembly.LoadFile(Application.StartupPath + "//" + dllName);
                                dllHash.Add(dllName, asmb);
                            }


                            Type T = asmb.GetType(attributeKey);
                            if (T != null)
                            {
                                object res = new System.Resources.ResourceManager(T).GetObject("$this.Icon");
                                try
                                {
                                    if (res != null)
                                    {
                                        Icon ic = new Icon((Icon)(res), new Size(16, 16));
                                        item.Settings.AppearancesSmall.Appearance.Image = ic.ToBitmap();
                                    }
                                    else
                                    {
                                        item.Settings.AppearancesSmall.Appearance.Image = defaultbitmap;
                                    }
                                }
                                catch
                                {
                                    item.Settings.AppearancesSmall.Appearance.Image = defaultbitmap;
                                }
                            }

                            //'===================================================
                            try
                            {
                                if (this.ueb.Groups[GroupKey].Settings.AppearancesSmall.HeaderAppearance.Image == null)
                                    this.ueb.Groups[GroupKey].Settings.AppearancesSmall.HeaderAppearance.Image = item.Settings.AppearancesSmall.Appearance.Image;
                            }
                            catch
                            {
                            }
                            item.Tag = T;
                            item.Key = attributeKey;
                            item.Text = attributeName;

                            this.AddGroupItem(item, GroupKey);

                        } //Enumerate Elements
                    } //Enumerate Groups
                }
            }
        }

        public void AddGroupItem(UltraExplorerBarItem item, string Group)
        {
            this.ueb.Groups[Group].Items.Add(item);
        }

        public void AddGroupSeparator(string Group)
        {
            this.ueb.Groups[Group].Items.Add().Settings.Style = ItemStyle.Separator;
        }

        public void AddGroupLabel(string Key, string Group)
        {
            this.ueb.Groups[Group].Items.Add(null, Key).Settings.Style = ItemStyle.Label;
        }

        private void ueb_ItemClick(object sender, Infragistics.Win.UltraWinExplorerBar.ItemEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                if (e.Item.Settings.Style == ItemStyle.Default)
                {
                    Type t = (Type)e.Item.Tag;// ht[e.Item.Key];

                    Billing.FormRegister.OpenMenuElement(t.Module.Name, t.FullName, this, Guid.Empty);
                }
            }
            finally
            { this.Cursor = Cursors.Default; }
        }
        #endregion

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.UpdateStatusInfo();

            using (DataObjects.ShellDataContext dc = new Billing.DataObjects.ShellDataContext())
            {

                var g = dc.GetMenuByUserID(Profile.UserGUID);
                string m = "";
                foreach (var item in g)
                {
                    m = item.Menu.ToString();
                }
                CreateMenu(m);
            }
            if (Program.IsFirstLogin)
            {
                PassChangeForm frm = new PassChangeForm(true);
                MessageBox.Show("პაროლი არ შეიძლება იყოს ცარიელი მნიშვნელობა, აუცილებელია პაროლის შეცვლა", "შეტყობინება");
                if (frm.ShowDialog() != DialogResult.OK)
                {
                    Application.Exit();
                }
            }
        }

        private void UpdateStatusInfo()
        {
            //StandardBank.DataAccess.DataProvider dp = new StandardBank.DataAccess.DataProvider();
            //System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(dp.ConnectionString);

            //System.Uri i = new Uri(new StandardBank.ApplicationProxy.ApplicationProxy().Url);


            //this.sbMain.Panels["Server"].Text = "Server: " + cn.DataSource + " WS: " + i.Host;
            //this.sbMain.Panels["Version"].Text = "Version: " + System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString();
            //this.sbMain.Panels["UserInfo"].Text = StandardBank.Shared.Profile.UserName;
        }
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!FormManager.AllowQuit)
            {
                FormManager.AllowQuit = true;
                e.Cancel = true;
                return;
            }

            if (this.MdiChildren.Length != 0)
            {

                if (MessageBox.Show("დავასრულო პროგრამაში მუშაობა?", "გამოსვლა", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.No)
                {
                    FormManager.AllowQuit = true;
                    e.Cancel = true;
                    return;
                }
            }
            Program.IsAuthorized = false;

            //global::StandardBank.Properties.Settings.Default.FormState = WindowState;
            //global::StandardBank.Properties.Settings.Default.Save();
        }

        private void TBM_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key.ToLower())
            {
                case "mnuexit":
                    this.Close();
                    break;
                case "mnuchangepassword":
                    using (PassChangeForm frm = new PassChangeForm())
                    {
                        frm.ShowDialog();
                    }
                    break;
                case "mnuabout":
                    //using (AboutForm f = new AboutForm())
                    //{
                    //    f.ShowDialog();
                    //}
                    break;
                default:
                    break;
            }
        }
    }
}
